package com.axter.libs.base.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * ListView适配器
 * Created by zhaobo on 2016/8/24.
 */
public abstract class CommonAdapter<T> extends BaseAdapter implements IAdapter<T>, IBaseAdapter<T> {
    private LayoutInflater mInflater;
    private List<T> mData;

    // 记录数据对象地址,防止数据变化导致位置变化
    private List<T> mCheckeds;
    // 服务于单选
    private int mLastPosition;
    private IBaseViewHolder mLastViewHolder;
    private int mLastViewHolderPosition;
    /**
     * 选择监听器 设置了就进入选择模式
     */
    private ICheckedInterface mCheckedInterface;
    /**
     * true 单选模式  false多选模式
     */
    private boolean mIsSingle = false;
    /**
     * 记录ViewHolder,给选择模式刷新数据用
     */
    private SparseArray<IBaseViewHolder> mViewHolders = new SparseArray<>();

    public CommonAdapter() {
        this(null);
    }

    public CommonAdapter(List<T> list) {
        if (list != null) {
            mData = list;
        } else {
            mData = new ArrayList<>();
        }
        mCheckeds = new ArrayList<>();
    }

    public void setCheckedInterface(ICheckedInterface checkedInterface) {
        mCheckedInterface = checkedInterface;
    }

    public void setCheckedMode(boolean isSingle) {
        mIsSingle = isSingle;
    }

    private void refreshChecked(int position, IBaseViewHolder viewHolder) {
        if (mCheckedInterface == null) {
            return;
        }
        // 可能刷新列表数据丢失了
        // 重置上一次选择的数据
        if (isChecked(position)) {
            setLastChecked(position, viewHolder);
        }
        mViewHolders.put(position, viewHolder);
    }

    public boolean isChecked(int position) {
        return mCheckeds.contains(getItem(position));
    }

    private void setLastChecked(int position, IBaseViewHolder viewHolder) {
        mLastPosition = position;
        mLastViewHolder = viewHolder;
        mLastViewHolderPosition = position;
    }

    public List<T> getChecked() {
        return mCheckeds;
    }

    public void setCheckedItem(int position) {
        if (mCheckedInterface == null) {
            return;
        }
        if (isChecked(position)) {
            // 取消选择
            if (mCheckedInterface == null || mCheckedInterface.isChecked(position, false)) {
                mCheckeds.remove(getItem(position));
                // 刷新ViewHolder
                onCheckedChanged(mViewHolders.get(position), position, false);
            }
        } else {
            if (mIsSingle) {
                mCheckeds.clear();
                // LastViewHolder
                if (mLastViewHolder != null && mViewHolders.get(mLastPosition) == mLastViewHolder) {
                    // 刷新ViewHolder
                    onCheckedChanged(mLastViewHolder, mLastPosition, false);
                }
            }
            // 选择
            if (mCheckedInterface == null || mCheckedInterface.isChecked(position, true)) {
                mCheckeds.add(getItem(position));
                // 刷新ViewHolder
                onCheckedChanged(mViewHolders.get(position), position, true);
                setLastChecked(position, mViewHolders.get(position));
            }
        }
    }

    protected void onCheckedChanged(IBaseViewHolder viewHolder, int position, boolean isChecked) {
        if (viewHolder != null && viewHolder instanceof IBaseCheckedViewHolder) {
            ((IBaseCheckedViewHolder) viewHolder).onCheckedChanged(getItem(position), position, isChecked);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        IBaseViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = createViewHolder(getItemViewType(position));

            convertView = AdapterUtils.getView(getLayoutInflater(parent.getContext()), parent, viewHolder.getContentView());
            convertView.setTag(viewHolder);

            viewHolder.bindViews(convertView);
        } else {
            viewHolder = (IBaseViewHolder) convertView.getTag();
        }
        refreshChecked(position, viewHolder);
        viewHolder.handleData(getItem(position), position);
        return convertView;
    }

    protected LayoutInflater getLayoutInflater(Context context) {
        if (mInflater == null) {
            mInflater = LayoutInflater.from(context);
        }
        return mInflater;
    }

    @Override
    public void add(T obj) {
        if (obj != null) {
            mData.add(obj);
        }
    }

    @Override
    public void addAll(List<T> objs) {
        if (objs != null && objs.size() != 0) {
            mData.addAll(objs);
        }
    }

    @Override
    public void clear() {
        mViewHolders.clear();
        if (mData != null) {
            mData.clear();
        } else {
            mData = new ArrayList<>();
        }
    }

    @Override
    public T getItem(int position) {
        return mData == null ? null : mData.get(position);
    }

    @Override
    public List<T> getItems() {
        return mData;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
